<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/15
 * Time: 11:42
 */

namespace app\admin\controller;


class Tag extends Base
{
    private $obj;
    public function _initialize()
    {
       $this -> obj = model('tag');
    }

    public function index()
    {
        //获取标签信息
        $tags = $this -> obj -> getTagsInfo();
        return $this -> fetch('',[
            'tags' => $tags
        ]);
    }

    public function add()
    {
        return $this->fetch();
    }

    public function save()
    {
        $data = input('post.');
        $validate = validate('Tag');
        if(!$validate->scene('add')->check($data)) {
            $this->error($validate->getError());
        }
        if(!empty($data['id'])) {
            return $this->update($data);
        }

        $res = $this->obj->add($data);
        if($res) {
            $this->success('新增成功');
        }else {
            $this->error('新增失败');
        }
    }
    //修改
    public function edit($id =0)
    {
        if(intval($id) <1){
            $this -> error('参数错误');
        }
        $res = $this -> obj -> get($id);
        return $this -> fetch('',[
            'tag' => $res
        ]);
    }

    public function update($data)
    {
        $res = $this -> obj -> save($data,['id' => intval($data['id'])]);
        if($res) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }

}